#include <windows.h>
#include <stdio.h>
#include <mmsystem.h>

#include "../resource.h"
#include "CSprite.h"
#include "CMap.h"

#define CLASSNAME "ScrapCraft"

#define MINIMAP_X	10
#define MINIMAP_Y	494

bool terminateApp = false;
bool appActive = false;
int p = 0;

HDC hdc, hMemDC;

CBMPSprite *spr_buildings;
CBMPSprite *spr_units;
CBMPSprite *spr_resources;

HBITMAP bmp_cmdBar;

CMap *map;
int mapX=0, mapY=0;

bool minimapClicking = false;

void loadImages()
{
	map = new CMap("data/maps/map1.raw");
	spr_buildings = new CBMPSprite("data/units/buildings.bmp",128,128,true);
	spr_units = new CBMPSprite("data/units/units.bmp",32,32,true);
	spr_resources = new CBMPSprite("data/units/resources.bmp",64,64,false);

	bmp_cmdBar = (HBITMAP)LoadImage(NULL, "data/interface/cmd_bar.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
}




void paintApp(HDC dc)
{
	//draw map
	map->paint(dc,mapX,mapY);

	//draw stuff
	for (int i=0;i<MAP_VISY;i++)
	{
		for (int j=0;j<MAP_VISX;j++)
		{
			int p = map->getTile(j+mapX,i+mapY);

			if (p==TILE_RES1)
				spr_resources->paint(dc, j*32,i*32, 0,0);
			if (p==TILE_RES2)
				spr_resources->paint(dc, j*32,i*32, 1,0);

			if (p>=TILE_START1)
				spr_buildings->paint(dc,j*32,i*32, 0, p-TILE_START1);
		}
	}


	//draw interface
	SelectObject(hMemDC, bmp_cmdBar);
	BitBlt(dc, 0, MAP_VISY*32, 800,120,hMemDC, 0,0, SRCCOPY);

	//draw minimap
	COLORREF col;
	for (int i=0;i<map->getHeight();i++)
	{
		for (int j=0;j<map->getWidth();j++)
		{
			switch (map->getTile(j,i))
			{
			case TILE_GRASS:
				col = 0x089152;
				break;
			case TILE_DIRT:
				col = 0x094781;
				break;
			case TILE_ROCK:
				col = 0x878787;
				break;
			case TILE_WATER:
				col = 0xfc8a10;
				break;
			case TILE_RES1:
				col = 0xf9ff49;
				break;
			case TILE_RES2:
				col = 0xea84fc;
				break;
			}

			SetPixel(dc, MINIMAP_X+j, MINIMAP_Y+i, col);
		}
	}

	MoveToEx(dc, MINIMAP_X+mapX, MINIMAP_Y+mapY, NULL);
	LineTo(dc, MINIMAP_X+mapX+MAP_VISX, MINIMAP_Y+mapY);
	LineTo(dc, MINIMAP_X+mapX+MAP_VISX, MINIMAP_Y+mapY+MAP_VISY);
	LineTo(dc, MINIMAP_X+mapX, MINIMAP_Y+mapY+MAP_VISY);
	LineTo(dc, MINIMAP_X+mapX, MINIMAP_Y+mapY);
}

void minimapClick(int x, int y)
{
	mapX = x-MAP_VISX/2;
	if (mapX<0)
		mapX = 0;
	if (mapX+MAP_VISX>=map->getWidth())
		mapX = map->getWidth()-MAP_VISX;

	mapY = y-MAP_VISY/2;
	if (mapY<0)
		mapY = 0;
	if (mapY+MAP_VISY>=map->getHeight())
		mapY = map->getHeight()-MAP_VISY;

}
LRESULT WINAPI MainProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	short xPos, yPos;

	switch( msg ) 
	{
		case WM_ACTIVATE:						
		{
			if (!HIWORD(wParam))				
			{
				appActive=TRUE;					
			}
			else
			{
				appActive=FALSE;					
			}

			return 0;							
		}

	case WM_CREATE:
		loadImages();
		return 0;

		case WM_CLOSE:						
		{
			PostQuitMessage(0);					
			return 0;							
		}

		case WM_KEYDOWN:
			if ((wParam == VK_UP)&&(mapY>0))
				mapY --;
			if ((wParam == VK_DOWN)&&((mapY+MAP_VISY)<(map->getHeight()-1)))
				mapY ++;
			if ((wParam == VK_LEFT)&&(mapX>0))
				mapX --;
			if ((wParam == VK_RIGHT)&&((mapX+MAP_VISX)<(map->getWidth()-1)))
				mapX ++;
			return 0;

		case WM_LBUTTONDOWN:
			xPos = (short)LOWORD(lParam); 
			yPos = (short)HIWORD(lParam);

			SetCapture(hWnd);

			if ((xPos>=MINIMAP_X)&&(xPos<MINIMAP_X+96)&&(yPos>=MINIMAP_Y)&&(yPos<MINIMAP_Y+96))
			{
				minimapClicking = true;
				minimapClick(xPos-MINIMAP_X, yPos-MINIMAP_Y);
			}
			return 0;

		case WM_LBUTTONUP:
			minimapClicking = false;

			ReleaseCapture();
			return 0;

		case WM_MOUSEMOVE:
			xPos = (short)LOWORD(lParam); 
			yPos = (short)HIWORD(lParam);

			if ((wParam & MK_LBUTTON)&&(minimapClicking))
			{
				minimapClick(xPos-MINIMAP_X, yPos-MINIMAP_Y);
			}
			return 0;
	}
	return( DefWindowProc( hWnd, msg, wParam, lParam ));
}

void mainLoop()
{
	MSG msg;

	WNDCLASS wc;
	HWND hWnd;
	HINSTANCE hInst = GetModuleHandle(NULL);

	wc.style = 0;
	wc.lpfnWndProc = MainProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInst;
	wc.hIcon = LoadIcon( hInst, MAKEINTRESOURCE(IDI_ICON1));
	wc.hCursor = LoadCursor( 0, IDI_APPLICATION );
	wc.hbrBackground = NULL;


	wc.lpszMenuName = NULL;
	wc.lpszClassName = CLASSNAME;
	if (!RegisterClass(&wc)) 
		exit(1);

	RECT r;
	r.left = 100;
	r.top = 100;
	r.right = r.left + 800;
	r.bottom = r.top + 600;
	DWORD wndStyle = WS_OVERLAPPEDWINDOW;

	AdjustWindowRect(&r, wndStyle, false);
	hWnd = CreateWindow( CLASSNAME, CLASSNAME,
		wndStyle,
		r.left, r.top, r.right-r.left, r.bottom-r.top,
		0, 0, hInst, NULL );


	if (!hWnd) 
		exit(1);
	ShowWindow( hWnd, SW_SHOW );
	UpdateWindow( hWnd );

	hdc = GetDC(hWnd);

	DWORD first = timeGetTime();
	int frames=0;

	HDC backDC = CreateCompatibleDC(hdc);
	hMemDC = CreateCompatibleDC(backDC);
	HBITMAP backBmp = CreateCompatibleBitmap(hdc, 800,600);
	SelectObject(backDC, backBmp);

	while(!terminateApp)
	{
		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			if (msg.message==WM_QUIT)	
			{
				terminateApp=TRUE;	
			}
			else						
			{
				TranslateMessage(&msg);		
				DispatchMessage(&msg);		
			}
		}
		else								
		{
			if (appActive)		
			{
				DWORD last = timeGetTime();

				paintApp(backDC);

				BitBlt(hdc, 0,0,800,600,backDC,0,0,SRCCOPY);

				DWORD cur = timeGetTime();
				long slp = (1000/40) - (cur-last);
				if (slp<1)
					slp = 1;
				frames++;
				if (cur-first>1000)
				{
					char buf[100];
					sprintf(buf,"%d fps",frames);
					frames=0;
					SetWindowText(hWnd, buf);
					first = cur;
				}

				last = cur;
				Sleep(slp);
			}
		}
	}

}